

export class Color32 {
    private values: Uint8ClampedArray = new Uint8ClampedArray(4);
    constructor(r: number, g: number, b: number, a: number = 255) {
        this.values[0] = r;
        this.values[1] = g;
        this.values[2] = b;
        this.values[3] = a;
    }
    get R(): number {
        return this.values[0];
    }
    get G(): number {
        return this.values[1];
    }
    get B(): number {
        return this.values[2];
    }
    get A(): number {
        return this.values[3];
    }
    set R(v: number) {
        this.values[0] = v;
    }
    set G(v: number) {
        this.values[1] = v;
    }
    set B(v: number) {
        this.values[2] = v;
    }
    set A(v: number) {
        this.values[3] = v;
    }
    static Equal(c: Color32, c2: Color32) {
        return (c.R == c2.R && c.G == c2.G && c.B == c2.B && c.A == c2.A)
    }
    toString(): string {
        return `rgba(${this.R},${this.G},${this.B},${this.A})`;
    }
    Clone(): Color32 {
        return new Color32(this.R, this.G, this.B, this.A);
    }
}
export class ColorF {
    r: number = 1;
    g: number = 1;
    b: number = 1;
    a: number = 1;
    ToColor32(): Color32 {
        return new Color32((this.r * 255) | 0, (this.g * 255) | 0, (this.b * 255) | 0, (this.a * 255) | 0);
    }
    public static FromH(h360: number): ColorF//输入一个0到360的值
    {
        let c = new ColorF();
        //h360 = 60
        //sin插值
        var v = (h360 % 120 / 120.0);
        if (v < 0) v += 1;

        var f = Math.sin(v * Math.PI) * 0.5;
        if (v > 0.5)
            f = 1 - f;
        //直接插值
        //let f = h360 % 120 / 120;
        if (h360 < 120)  //b->r
        {

            if (f <= 0.5) {
                c.b = 1.0;
                c.r = (f) / (1 - f);
            }
            else {
                c.r = 1.0;
                c.b = (1 - f) / f;
            }
            c.g = 0;
        }
        else if (h360 < 240) //r->g
        {

            if (f <= 0.5) {
                c.r = 1.0;
                c.g = 1.0 * f / (1 - f);
            }
            else {
                c.g = 1.0;
                c.r = 1.0 * (1 - f) / f;
            }
            c.b = 0;

        }
        else //g->b
        {

            c.r = 0;
            if (f <= 0.5) {
                c.g = 1.0;
                c.b = 1.0 * f / (1 - f);
            }
            else {
                c.b = 1.0;
                c.g = 1.0 * (1 - f) / f;
            }
        }
        return c;
    }
}
export class Palette {
    private static nespal: Color32[]
    static getNesPal() {
        if (this.nespal == null) {
            this.nespal = [];
            this.nespal.push(new Color32(0, 0, 0, 0));
            this.nespal.push(new Color32(0, 0, 0, 255));
            //把下面的颜色值，均参照上面的格式写，alpha都是255
            this.nespal.push(new Color32(121, 121, 121, 255));
            this.nespal.push(new Color32(162, 162, 162, 255));
            this.nespal.push(new Color32(48, 81, 130, 255));
            this.nespal.push(new Color32(65, 146, 195, 255));
            this.nespal.push(new Color32(97, 211, 227, 255));
            this.nespal.push(new Color32(162, 255, 243, 255));
            this.nespal.push(new Color32(48, 97, 65, 255));
            this.nespal.push(new Color32(73, 162, 105, 255));
            this.nespal.push(new Color32(113, 227, 146, 255));
            this.nespal.push(new Color32(162, 255, 203, 255));
            this.nespal.push(new Color32(56, 109, 0, 255));
            this.nespal.push(new Color32(73, 170, 16, 255));
            this.nespal.push(new Color32(113, 243, 65, 255));
            this.nespal.push(new Color32(162, 243, 162, 255));
            this.nespal.push(new Color32(56, 105, 0, 255));
            this.nespal.push(new Color32(81, 162, 0, 255));
            this.nespal.push(new Color32(154, 235, 0, 255));
            this.nespal.push(new Color32(203, 243, 130, 255));
            this.nespal.push(new Color32(73, 89, 0, 255));
            this.nespal.push(new Color32(138, 138, 0, 255));
            this.nespal.push(new Color32(235, 211, 32, 255));
            this.nespal.push(new Color32(255, 243, 146, 255));
            this.nespal.push(new Color32(121, 65, 0, 255));
            this.nespal.push(new Color32(195, 113, 0, 255));
            this.nespal.push(new Color32(255, 162, 0, 255));
            this.nespal.push(new Color32(255, 219, 162, 255));
            this.nespal.push(new Color32(162, 48, 0, 255));
            this.nespal.push(new Color32(227, 81, 0, 255));
            this.nespal.push(new Color32(255, 121, 48, 255));
            this.nespal.push(new Color32(255, 203, 186, 255));
            this.nespal.push(new Color32(178, 16, 48));
            this.nespal.push(new Color32(219, 65, 97));
            this.nespal.push(new Color32(255, 97, 178));
            this.nespal.push(new Color32(255, 186, 235));
            this.nespal.push(new Color32(154, 32, 121));
            this.nespal.push(new Color32(219, 65, 195));
            this.nespal.push(new Color32(243, 97, 255));
            this.nespal.push(new Color32(227, 178, 255));
            this.nespal.push(new Color32(97, 16, 162));
            this.nespal.push(new Color32(146, 65, 243));
            this.nespal.push(new Color32(162, 113, 255));
            this.nespal.push(new Color32(195, 178, 255));
            this.nespal.push(new Color32(40, 0, 186));
            this.nespal.push(new Color32(65, 65, 255));
            this.nespal.push(new Color32(81, 130, 255));
            this.nespal.push(new Color32(162, 186, 255));
            this.nespal.push(new Color32(32, 0, 178));
            this.nespal.push(new Color32(65, 97, 251));
            this.nespal.push(new Color32(97, 162, 255));
            this.nespal.push(new Color32(146, 211, 255));
            this.nespal.push(new Color32(121, 121, 121));
            this.nespal.push(new Color32(178, 178, 178));
            this.nespal.push(new Color32(235, 235, 235));
            this.nespal.push(new Color32(255, 255, 255));

        }
        return this.nespal;
    }
}